package org.alvin.code.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 */
public class RegexUtils {
    /**
     * 查询单个值
     *
     * @param regex
     * @param text
     * @return
     */
    public static String group(String regex, String text) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    /**
     * 获取一组值
     *
     * @param regex
     * @param text
     * @return
     */
    public static List<String> groups(String regex, String text) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        List<String> groups = new ArrayList<>();
        while (matcher.find()) {
            groups.add(matcher.group(1));
        }
        return groups;
    }

    /**
     * 返回key value map
     *
     * @param regex
     * @param text
     * @return
     */
    public static Map<String, String> groupKeyValue(String regex, String text) {
        Map<String, String> res = new HashMap<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            res.put(matcher.group(1), matcher.group(2));
        }
        return res;
    }

    /**
     * 保持前缀
     *
     * @param regex
     * @param text
     * @param replacement
     * @return
     */
    public static String replaceAllKeepPrefix(String regex, String text, String replacement) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.replaceAll("$0".concat(replacement));
    }

    /**
     * 保持后缀
     *
     * @param regex
     * @param text
     * @param replacement
     * @return
     */
    public static String replaceAllKeepSuffix(String regex, String text, String replacement) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.replaceAll(replacement.concat("$0"));
    }

    /**
     * 保持前后信息
     *
     * @param regex
     * @param text
     * @param replacement
     * @return
     */
    public static String replaceAllKeepAll(String regex, String text, String replacement) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.replaceAll("$1".concat(replacement).concat("$2"));
    }

//    public static void main(String[] args) {
        // test group
//        String text ="tangzhichao@163.com";
//        String regex = "(\\w+)@.+";
//        String res = group(regex,text);
//        System.out.println(res);
        // test groups
//        String text ="a=1,b=2 c=3;dd=2";
//        String regex = "\\w+=(\\w+)";
//        System.out.println(groups(regex,text));
        // test keyValue
//        String text = "id=11,name=\"2222\" mail=\"test@123.com\"";
//        String regex = "(\\w+)=\"?(\\w+)\"?";
//        System.out.println(groupKeyValue(regex,text));
//        前后缀
//        String text ="package com.test.test;";
//        String regex ="package.+;";
//        String replacement = "\nimport java.util.Date;";
//        System.out.println(replaceAllKeepPrefix(regex,text,replacement));
//        System.out.println(replaceAllKeepSuffix(regex,text,replacement));
        //保持前后缀
//        String text = "package com.test.test;";
//        String regex = "(package).+(;)";
//        String replacement = " java.util.Date ";
//        System.out.println(replaceAllKeepAll(regex, text, replacement));
//    }
}
